package mygameclient;

public class ClientCommands {

    public static GameClient clientConnection;

    public static void connectGame(String myName) {
        clientConnection = new GameClient();
        clientConnection.sendMessage(CommandsList.CONNECT.getAbbr() + ";" + myName);
    }

    public static void closeConnection() {
        if (clientConnection != null) {
            clientConnection.sendMessage(CommandsList.CLOSE_CONNECTION.getAbbr());
            clientConnection.kill();
        }
    }

    public static void setCell(int i, int j, int cellType, int gameIndex) {
        clientConnection.sendMessage(CommandsList.SET_CELL.getAbbr() + ";" + i + ";" + j + ";" + cellType + ";" + gameIndex + ";");
    }

    public static void gameRequest(int playerCode) {
        clientConnection.sendMessage(CommandsList.GAME_REQUEST.getAbbr() + ";" + playerCode);
    }

    public static void resumeGame(int gameIndex, int answer) {
        clientConnection.sendMessage(CommandsList.GAME_RESUME.getAbbr() + ";" + gameIndex + ";" + answer);
    }

    public static void receiveStart(String[] arg) {
        MessageBox.ok("Your turn");
        GamePanel.start = true;
        GamePanel.gameIndex = Integer.parseInt(arg[1]);
        GamePanel.cellType = 1;
        ClientUI.gamePanel.erasePanel();
    }

    public static void receiveInit(String[] arg) {
        GamePanel.gameIndex = Integer.parseInt(arg[1]);
        GamePanel.cellType = 0;
        ClientUI.gamePanel.erasePanel();
    }

    public static void receiveUpdate(String[] arg) {
        int i = 1;
        PlayersPanel.players.clear();
        while (i < arg.length - 1) {
            Player newPlayer = new Player(arg[i], Integer.parseInt(arg[i + 1]));
            (PlayersPanel.players).add(newPlayer);
            i += 2;
        }
        ClientUI.playersPanel.repaint();
    }

    public static void receiveCell(String[] arg) {
        int i = Integer.parseInt(arg[1]);
        int j = Integer.parseInt(arg[2]);
        int cellType = Integer.parseInt(arg[3]);
        ClientUI.gamePanel.paintCell(GamePanel.cells[i][j], cellType);
        GamePanel.cells[i][j].setEditable(false);

        int winner = Integer.parseInt(arg[4]);
        int currentPlayerCode = Integer.parseInt(arg[5]);

        if (winner == 0) {
            int answer = MessageBox.yesno("Nobody wins ! Play more ?");
            resumeGame(GamePanel.gameIndex, answer);

        } else if (winner == GamePanel.thisPlayerCode) {
            int answer = MessageBox.yesno("you win");
            resumeGame(GamePanel.gameIndex, answer);

        } else if (winner > 0) {
            int answer = MessageBox.yesno("Player [" + winner + "] win");
            resumeGame(GamePanel.gameIndex, answer);

        } else if (currentPlayerCode == GamePanel.thisPlayerCode) {
            MessageBox.ok("Your turn");
            GamePanel.start = true;
        }
    }

    public static void receiveRequest(String[] arg) {
        int answer = MessageBox.yesno("Player " + arg[1] + " want to play with you");
        clientConnection.sendMessage(CommandsList.APPROVE.getAbbr() + ";" + arg[1] + ";" + arg[2] + ";" + answer);
    }

    public static void receiveHash(String hash) {
        int hashCode = Integer.parseInt(hash);
        ClientUI.gamePanel.setThisPlayerCode(hashCode);
    }

    public static void addPlayerToList(String[] arg) {
        Player player = new Player(arg[1], Integer.parseInt(arg[2]));
        PlayersPanel.players.add(player);
        ClientUI.playersPanel.repaint();
    }

    public static void removePlayerFromList(String[] arg) {
        Player player = null;
        for (Player p : PlayersPanel.players) {
            if (p.getpHashCode() == Integer.parseInt(arg[2])) {
                player = p;
                break;
            }
        }
        PlayersPanel.players.remove(player);
        ClientUI.playersPanel.repaint();
    }

    public static void receive(String inp) {
        String[] arg = inp.split("\\;");
        CommandsList comm = CommandsList.getCommand(arg[0]);

        switch (comm) {
            case UPDATE:
                receiveUpdate(arg);
                break;
            case HASH_CODE:
                receiveHash(arg[1]);
                break;
            case ADD_PLAYER:
                addPlayerToList(arg);
                break;
            case REMOVE_PLAYER:
                removePlayerFromList(arg);
                break;
            case START_GAME:
                if (ClientUI.gamePanel.getThisPlayerCode() == Integer.parseInt(arg[2])) {
                    receiveStart(arg);
                } else {
                    receiveInit(arg);
                }
                break;
            case GAME_REQUEST:
                receiveRequest(arg);
                break;
            case SET_CELL:
                receiveCell(arg);
                break;
        }
    }
}
